<template><div><h2 id="国际化" tabindex="-1"><a class="header-anchor" href="#国际化"><span>国际化</span></a></h2>
<p>国际化（I18N），指的是设计软件应用程序的过程，使其能够适应各种语言和地区，而无需进行工程更改。对于 Web 应用程序来说，这一点尤为重要，因为潜在用户可能遍布全球。Yii 提供了全面的 I18N 特性，支持消息翻译、视图翻译、日期和数字格式等。</p>
<h2 id="区域和语言" tabindex="-1"><a class="header-anchor" href="#区域和语言"><span>区域和语言</span></a></h2>
<h3 id="区域" tabindex="-1"><a class="header-anchor" href="#区域"><span>区域</span></a></h3>
<p>Locale is a set of parameters that defines the user’s language, country and any special variant preferences that the user wants to see in their user interface. It is usually identified by an ID consisting of a language ID and a region ID.</p>
<p>For example, the ID <code v-pre>en-US</code> stands for the locale of “English and the United States”.</p>
<p>For consistency reasons, all locale IDs used in Yii applications should be canonicalized to the format of <code v-pre>ll-CC</code>, where <code v-pre>ll</code> is a two- or three-letter lowercase language code according to <a href="https://www.loc.gov/standards/iso639-2/" target="_blank" rel="noopener noreferrer">ISO-639</a> and <code v-pre>CC</code> is a two-letter country code according to <a href="https://en.wikipedia.org/wiki/ISO_3166-1#Current_codes" target="_blank" rel="noopener noreferrer">ISO-3166</a>. More details about locale can be found in the <a href="https://unicode-org.github.io/icu/userguide/locale/#the-locale-concept" target="_blank" rel="noopener noreferrer">documentation of the ICU project</a>.</p>
<h3 id="语言" tabindex="-1"><a class="header-anchor" href="#语言"><span>语言</span></a></h3>
<p>In Yii, we often use the term “language” to refer to a locale.</p>
<p>A Yii application uses two kinds of languages:</p>
<ul>
<li>[[yii\base\Application::$sourceLanguage|source language]]: This refers to the language in which the text messages in the source code are written.</li>
<li>[[yii\base\Application::$language|target language]]: This is the language that should be used to display content to end users.</li>
</ul>
<p>The so-called message translation service mainly translates a text message from source language to target language.</p>
<h3 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h3>
<p>You can configure application languages in the “application configuration” like the following:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// set target language to be Russian</span></span>
<span class="line">    <span class="token string single-quoted-string">'language'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'ru-RU'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// set source language to be English</span></span>
<span class="line">    <span class="token string single-quoted-string">'sourceLanguage'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'en-US'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token operator">...</span><span class="token operator">...</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>The default value for the [[yii\base\Application::$sourceLanguage|source language]] is <code v-pre>en-US</code>, meaning US English. It is <strong>recommended</strong> that you keep this default value unchanged. Usually it is much easier to find people who can translate from “English to other languages” than from “non-English to non-English”.</p>
<p>You often need to set the [[yii\base\Application::$language|target language]] dynamically based on different factors, such as the language preference of end users. Instead of configuring it in the application configuration, you can use the following statement to change the target language:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// change target language to Chinese</span></span>
<span class="line"><span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">language</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'zh-CN'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Tip: If your source language varies among different parts of your code, you can override the source language for different message sources, which are described in the next section.</p>
</blockquote>
<h2 id="消息翻译" tabindex="-1"><a class="header-anchor" href="#消息翻译"><span>消息翻译</span></a></h2>
<h3 id="从源语言到目标语言" tabindex="-1"><a class="header-anchor" href="#从源语言到目标语言"><span>从源语言到目标语言</span></a></h3>
<p>The message translation service translates a text message from one language (usually the [[yii\base\Application::$sourceLanguage|source language]]) to another (usually the [[yii\base\Application::$language|target language]]).</p>
<p>It does the translation by looking up the message to be translated in a message source which stores the original messages and the translated messages. If the message is found, the corresponding translated message will be returned; otherwise the original message will be returned untranslated.</p>
<h3 id="如何实现" tabindex="-1"><a class="header-anchor" href="#如何实现"><span>如何实现</span></a></h3>
<p>To use the message translation service, you mainly need to do the following work:</p>
<ol>
<li>Wrap every text message that needs to be translated in a call to the [[Yii::t()]] method.</li>
<li>Configure one or multiple message sources in which the message translation service can look for translated messages.</li>
<li>Let the translators translate messages and store them in the message source(s).</li>
</ol>
<h4 id="_1-wrap-a-text-message" tabindex="-1"><a class="header-anchor" href="#_1-wrap-a-text-message"><span>1. Wrap a text message</span></a></h4>
<p>The method [[Yii::t()]] can be used like the following,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'This is a string to translate!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>where the second parameter refers to the text message to be translated, while the first parameter refers to the name of the category which is used to categorize the message.</p>
<h4 id="_2-configure-one-or-multiple-message-sources" tabindex="-1"><a class="header-anchor" href="#_2-configure-one-or-multiple-message-sources"><span>2. Configure one or multiple message sources</span></a></h4>
<p>The [[Yii::t()]] method will call the <code v-pre>i18n</code> <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/structure-application-components.md" target="_blank" rel="noopener noreferrer">application component</a> <code v-pre>translate</code> method to perform the actual translation work. The component can be configured in the application configuration as follows,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'i18n'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'translations'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'app*'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\i18n\PhpMessageSource'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token comment">//'basePath' => '@app/messages',</span></span>
<span class="line">                <span class="token comment">//'sourceLanguage' => 'en-US',</span></span>
<span class="line">                <span class="token string single-quoted-string">'fileMap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'app'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app.php'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'app/error'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'error.php'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>In the above code, a message source supported by [[yii\i18n\PhpMessageSource]] is being configured.</p>
<h5 id="category-wildcards-with-symbol" tabindex="-1"><a class="header-anchor" href="#category-wildcards-with-symbol"><span>Category wildcards with <code v-pre>*</code> symbol</span></a></h5>
<p>The pattern <code v-pre>app*</code> indicates that all message categories whose names start with <code v-pre>app</code> should be translated using this message source.</p>
<h4 id="_3-let-the-translators-translate-messages-and-store-them-in-the-message-source-s" tabindex="-1"><a class="header-anchor" href="#_3-let-the-translators-translate-messages-and-store-them-in-the-message-source-s"><span>3. Let the translators translate messages and store them in the message source(s)</span></a></h4>
<p>The [[yii\i18n\PhpMessageSource]] class uses PHP files with a simple PHP array to store message translations. These files contain a map of the messages in <code v-pre>source language</code> to the translation in the <code v-pre>target language</code>.</p>
<blockquote>
<p>Info: You can automatically generate these PHP files by using the <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-i18n.md#message-command" target="_blank" rel="noopener noreferrer"><code v-pre>message</code> command</a>, which will be introduced later in this chapter.</p>
</blockquote>
<p>Each PHP file corresponds to the messages of a single category. By default, the file name should be the same as the category name. Example for <code v-pre>app/messages/nl-NL/main.php:</code></p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line">* Translation map for nl-NL</span>
<span class="line">*/</span></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'welkom'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h5 id="file-mapping" tabindex="-1"><a class="header-anchor" href="#file-mapping"><span>File mapping</span></a></h5>
<p>You may configure [[yii\i18n\PhpMessageSource::fileMap|fileMap]] to map a category to a PHP file with a different naming approach.</p>
<p>In the above example, the category <code v-pre>app/error</code> is mapped to the PHP file <code v-pre>@app/messages/ru-RU/error.php</code> (assuming <code v-pre>ru-RU</code> is the target language). However, without this configuration the category would be mapped to <code v-pre>@app/messages/ru-RU/app/error.php</code> instead.</p>
<h5 id="other-storage-types" tabindex="-1"><a class="header-anchor" href="#other-storage-types"><span>Other storage types</span></a></h5>
<p>Besides storing the messages in PHP files, you may also use the following message sources to store translated messages in different storage:</p>
<ul>
<li>[[yii\i18n\GettextMessageSource]] uses GNU Gettext MO or PO files to maintain translated messages.</li>
<li>[[yii\i18n\DbMessageSource]] uses a database table to store translated messages.</li>
</ul>
<h2 id="消息格式化" tabindex="-1"><a class="header-anchor" href="#消息格式化"><span>消息格式化</span></a></h2>
<p>When translating a message, you can embed some placeholders and have them replaced by dynamic parameter values. You can even use special placeholder syntax to have the parameter values formatted according to the target language. In this subsection, we will describe different ways of formatting messages.</p>
<h3 id="消息参数" tabindex="-1"><a class="header-anchor" href="#消息参数"><span>消息参数</span></a></h3>
<p>In a message to be translated, you can embed one or multiple parameters (also called placeholders) so that they can be replaced by the given values. By giving different sets of values, you can variate the translated message dynamically. In the following example, the placeholder <code v-pre>{username}</code> in the message <code v-pre>'Hello, {username}!'</code> will be replaced by <code v-pre>'Alexander'</code> and <code v-pre>'Qiang'</code>, respectively.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$username</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Alexander'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// display a translated message with username being "Alexander"</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Hello, {username}!'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token variable">$username</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$username</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Qiang'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// display a translated message with username being "Qiang"</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Hello, {username}!'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token variable">$username</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>While translating a message containing placeholders, you should leave the placeholders as is. This is because the placeholders will be replaced with the actual values when you call <code v-pre>Yii::t()</code> to translate a message.</p>
<p>You can use either <em>named placeholders</em> or <em>positional placeholders</em>, but not both, in a single message.</p>
<p>The previous example shows how you can use named placeholders. That is, each placeholder is written in the format of <code v-pre>{name}</code>, and you provide an associative array whose keys are the placeholder names (without the curly brackets) and whose values are the corresponding values placeholder to be replaced with.</p>
<p>Positional placeholders use zero-based integer sequence as names which are replaced by the provided values according to their positions in the call of <code v-pre>Yii::t()</code>. In the following example, the positional placeholders <code v-pre>{0}</code>, <code v-pre>{1}</code> and <code v-pre>{2}</code> will be replaced by the values of <code v-pre>$price</code>, <code v-pre>$count</code> and <code v-pre>$subtotal</code>, respectively.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$price</span> <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$count</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$subtotal</span> <span class="token operator">=</span> <span class="token number">200</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Price: {0}, Count: {1}, Subtotal: {2}'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token variable">$price</span><span class="token punctuation">,</span> <span class="token variable">$count</span><span class="token punctuation">,</span> <span class="token variable">$subtotal</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>In case of a single positional parameter its value could be specified without wrapping it into array:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Price: {0}'</span><span class="token punctuation">,</span> <span class="token variable">$price</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>Tip: In most cases you should use named placeholders. This is because the names will make the translators understand better the whole messages being translated.</p>
</blockquote>
<h3 id="参数格式" tabindex="-1"><a class="header-anchor" href="#参数格式"><span>参数格式</span></a></h3>
<p>You can specify additional formatting rules in the placeholders of a message so that the parameter values can be formatted properly before they replace the placeholders. In the following example, the price parameter value will be treated as a number and formatted as a currency value:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$price</span> <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Price: {0,number,currency}'</span><span class="token punctuation">,</span> <span class="token variable">$price</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: Parameter formatting requires the installation of the <a href="https://www.php.net/manual/en/intro.intl.php" target="_blank" rel="noopener noreferrer">intl PHP extension</a>.</p>
</blockquote>
<p>You can use either the short form or the full form to specify a placeholder with formatting:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">short form<span class="token punctuation">:</span> <span class="token punctuation">{</span>name<span class="token punctuation">,</span>type<span class="token punctuation">}</span></span>
<span class="line">full form<span class="token punctuation">:</span> <span class="token punctuation">{</span>name<span class="token punctuation">,</span>type<span class="token punctuation">,</span>style<span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: If you need to use special characters such as <code v-pre>{</code>, <code v-pre>}</code>, <code v-pre>'</code>, <code v-pre>#</code>, wrap them in <code v-pre>'</code>:</p>
</blockquote>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"Example of string with ''-escaped characters'': '{' '}' '{test}' {count,plural,other{''count'' value is # '#{}'}}"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'count'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Complete format is described in the <a href="https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classMessageFormat.html" target="_blank" rel="noopener noreferrer">ICU documentation</a>. In the following we will show some common usages.</p>
<h4 id="number" tabindex="-1"><a class="header-anchor" href="#number"><span>Number</span></a></h4>
<p>The parameter value is treated as a number. For example,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$sum</span> <span class="token operator">=</span> <span class="token number">42</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Balance: {0,number}'</span><span class="token punctuation">,</span> <span class="token variable">$sum</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>You can specify an optional parameter style as <code v-pre>integer</code>, <code v-pre>currency</code>, or <code v-pre>percent</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$sum</span> <span class="token operator">=</span> <span class="token number">42</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Balance: {0,number,currency}'</span><span class="token punctuation">,</span> <span class="token variable">$sum</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>You can also specify a custom pattern to format the number. For example,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$sum</span> <span class="token operator">=</span> <span class="token number">42</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Balance: {0,number,,000,000000}'</span><span class="token punctuation">,</span> <span class="token variable">$sum</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>Characters used in the custom format could be found in <a href="https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classDecimalFormat.html" target="_blank" rel="noopener noreferrer">ICU API reference</a> under “Special Pattern Characters” section.</p>
<p>The value is always formatted according to the locale you are translating to i.e. you cannot change decimal or thousands separators, currency symbol etc. without changing translation locale. If you need to customize these you can use [[yii\i18n\Formatter::asDecimal()]] and [[yii\i18n\Formatter::asCurrency()]].</p>
<h4 id="date" tabindex="-1"><a class="header-anchor" href="#date"><span>Date</span></a></h4>
<p>The parameter value should be formatted as a date. For example,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Today is {0,date}'</span><span class="token punctuation">,</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>You can specify an optional parameter style as <code v-pre>short</code>, <code v-pre>medium</code>, <code v-pre>long</code>, or <code v-pre>full</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Today is {0,date,short}'</span><span class="token punctuation">,</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>You can also specify a custom pattern to format the date value:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Today is {0,date,yyyy-MM-dd}'</span><span class="token punctuation">,</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><a href="https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classicu_1_1SimpleDateFormat.html#details" target="_blank" rel="noopener noreferrer">Formatting reference</a>.</p>
<h4 id="time" tabindex="-1"><a class="header-anchor" href="#time"><span>Time</span></a></h4>
<p>The parameter value should be formatted as a time. For example,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'It is {0,time}'</span><span class="token punctuation">,</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>You can specify an optional parameter style as <code v-pre>short</code>, <code v-pre>medium</code>, <code v-pre>long</code>, or <code v-pre>full</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'It is {0,time,short}'</span><span class="token punctuation">,</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>You can also specify a custom pattern to format the time value:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'It is {0,date,HH:mm}'</span><span class="token punctuation">,</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><a href="https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classicu_1_1SimpleDateFormat.html#details" target="_blank" rel="noopener noreferrer">Formatting reference</a>.</p>
<h4 id="spellout" tabindex="-1"><a class="header-anchor" href="#spellout"><span>Spellout</span></a></h4>
<p>The parameter value should be treated as a number and formatted as a spellout. For example,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// may produce "42 is spelled as forty-two"</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'{n,number} is spelled as {n,spellout}'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'n'</span> <span class="token operator">=></span> <span class="token number">42</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>By default the number is spelled out as cardinal. It could be changed:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// may produce "I am forty-seventh agent"</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'I am {n,spellout,%spellout-ordinal} agent'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'n'</span> <span class="token operator">=></span> <span class="token number">47</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>Note that there should be no space after <code v-pre>spellout,</code> and before <code v-pre>%</code>.</p>
<p>To get a list of options available for locale you’re using check “Numbering schemas, Spellout” at <a href="https://intl.rmcreative.ru/" target="_blank" rel="noopener noreferrer">intl.rmcreative.ru/</a>.</p>
<h4 id="ordinal" tabindex="-1"><a class="header-anchor" href="#ordinal"><span>Ordinal</span></a></h4>
<p>The parameter value should be treated as a number and formatted as an ordinal name. For example,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// may produce "You are the 42nd visitor here!"</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'You are the {n,ordinal} visitor here!'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'n'</span> <span class="token operator">=></span> <span class="token number">42</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>Ordinal supports more ways of formatting for languages such as Spanish:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// may produce 471ª</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'{n,ordinal,%digits-ordinal-feminine}'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'n'</span> <span class="token operator">=></span> <span class="token number">471</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>Note that there should be no space after <code v-pre>ordinal,</code> and before <code v-pre>%</code>.</p>
<p>To get a list of options available for locale you’re using check “Numbering schemas, Ordinal” at <a href="https://intl.rmcreative.ru/" target="_blank" rel="noopener noreferrer">intl.rmcreative.ru/</a>.</p>
<h4 id="duration" tabindex="-1"><a class="header-anchor" href="#duration"><span>Duration</span></a></h4>
<p>The parameter value should be treated as the number of seconds and formatted as a time duration string. For example,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// may produce "You are here for 47 sec. already!"</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'You are here for {n,duration} already!'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'n'</span> <span class="token operator">=></span> <span class="token number">47</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>Duration supports more ways of formatting:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// may produce 130:53:47</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'{n,duration,%in-numerals}'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'n'</span> <span class="token operator">=></span> <span class="token number">471227</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>Note that there should be no space after <code v-pre>duration,</code> and before <code v-pre>%</code>.</p>
<p>To get a list of options available for locale you’re using check “Numbering schemas, Duration” at <a href="https://intl.rmcreative.ru/" target="_blank" rel="noopener noreferrer">intl.rmcreative.ru/</a>.</p>
<h4 id="plural" tabindex="-1"><a class="header-anchor" href="#plural"><span>Plural</span></a></h4>
<p>Different languages have different ways to inflect plurals. Yii provides a convenient way for translating messages in different plural forms that works well even for very complex rules. Instead of dealing with the inflection rules directly, it is sufficient to provide the translation of inflected words in certain situations only. For example,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// When $n = 0, it may produce "There are no cats!"</span></span>
<span class="line"><span class="token comment">// When $n = 1, it may produce "There is one cat!"</span></span>
<span class="line"><span class="token comment">// When $n = 42, it may produce "There are 42 cats!"</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'There {n,plural,=0{are no cats} =1{is one cat} other{are # cats}}!'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'n'</span> <span class="token operator">=></span> <span class="token variable">$n</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>In the plural rule arguments above, <code v-pre>=</code> means explicit value. So <code v-pre>=0</code> means exactly zero, <code v-pre>=1</code> means exactly one. <code v-pre>other</code> stands for any other value. <code v-pre>#</code> is replaced with the value of <code v-pre>n</code> formatted according to target language.</p>
<p>Plural forms can be very complicated in some languages. In the following Russian example, <code v-pre>=1</code> matches exactly <code v-pre>n = 1</code> while <code v-pre>one</code> matches <code v-pre>21</code> or <code v-pre>101</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">Здесь <span class="token punctuation">{</span>n<span class="token punctuation">,</span>plural<span class="token punctuation">,</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">{</span>котов нет<span class="token punctuation">}</span> <span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">{</span>есть один кот<span class="token punctuation">}</span> one<span class="token punctuation">{</span><span class="token comment"># кот} few{# кота} many{# котов} other{# кота}}!</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>These <code v-pre>other</code>, <code v-pre>few</code>, <code v-pre>many</code> and other special argument names vary depending on language. To learn which ones you should specify for a particular locale, please refer to “Plural Rules, Cardinal” at <a href="https://intl.rmcreative.ru/" target="_blank" rel="noopener noreferrer">intl.rmcreative.ru/</a>. Alternatively you can refer to <a href="https://cldr.unicode.org/index/cldr-spec/plural-rules" target="_blank" rel="noopener noreferrer">rules reference at unicode.org</a>.</p>
<blockquote>
<p>Note: The above example Russian message is mainly used as a translated message, not an original message, unless you set the [[yii\base\Application::$sourceLanguage|source language]] of your application as <code v-pre>ru-RU</code> and translating from Russian.</p>
<p>When a translation is not found for an original message specified in <code v-pre>Yii::t()</code> call, the plural rules for the [[yii\base\Application::$sourceLanguage|source language]] will be applied to the original message.</p>
</blockquote>
<p>There’s an <code v-pre>offset</code> parameter for the cases when the string is like the following:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$likeCount</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'You {likeCount,plural,</span>
<span class="line">    offset: 1</span>
<span class="line">    =0{did not like this}</span>
<span class="line">    =1{liked this}</span>
<span class="line">    one{and one other person liked this}</span>
<span class="line">    other{and # others liked this}</span>
<span class="line">}'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'likeCount'</span> <span class="token operator">=></span> <span class="token variable">$likeCount</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// You and one other person liked this</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ordinal-selection" tabindex="-1"><a class="header-anchor" href="#ordinal-selection"><span>Ordinal selection</span></a></h4>
<p>The parameter type of <code v-pre>selectordinal</code> is meant to choose a string based on language rules for ordinals for the locale you are translating to:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$n</span> <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'You are the {n,selectordinal,one{#st} two{#nd} few{#rd} other{#th}} visitor'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'n'</span> <span class="token operator">=></span> <span class="token variable">$n</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// For English it outputs:</span></span>
<span class="line"><span class="token comment">// You are the 3rd visitor</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Translation</span></span>
<span class="line"><span class="token string single-quoted-string">'You are the {n,selectordinal,one{#st} two{#nd} few{#rd} other{#th}} visitor'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Вы {n,selectordinal,other{#-й}} посетитель'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// For Russian translation it outputs:</span></span>
<span class="line"><span class="token comment">// Вы 3-й посетитель</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>The format is very close to what’s used for plurals. To learn which arguments you should specify for a particular locale, please refer to “Plural Rules, Ordinal” at <a href="https://intl.rmcreative.ru/" target="_blank" rel="noopener noreferrer">intl.rmcreative.ru/</a>. Alternatively you can refer to <a href="https://unicode-org.github.io/cldr-staging/charts/37/supplemental/language_plural_rules.html" target="_blank" rel="noopener noreferrer">rules reference at unicode.org</a>.</p>
<h4 id="selection" tabindex="-1"><a class="header-anchor" href="#selection"><span>Selection</span></a></h4>
<p>You can use the <code v-pre>select</code> parameter type to choose a phrase based on the parameter value. For example,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// It may produce "Snoopy is a dog and it loves Yii!"</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'{name} is a {gender} and {gender,select,female{she} male{he} other{it}} loves Yii!'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Snoopy'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'gender'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'dog'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>In the expression above, both <code v-pre>female</code> and <code v-pre>male</code> are possible parameter values, while <code v-pre>other</code> handles values that do not match either one of them. Following each possible parameter value, you should specify a phrase and enclose it in a pair of curly brackets.</p>
<h3 id="specifying-default-message-source" tabindex="-1"><a class="header-anchor" href="#specifying-default-message-source"><span>Specifying default message source</span></a></h3>
<p>You can specify default message source that will be used as a fallback for category that doesn’t match any configured category. You can do that by configuring a wildcard category <code v-pre>*</code>. In order to do that, add the following to the application config:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">//configure i18n component</span></span>
<span class="line"></span>
<span class="line"><span class="token string single-quoted-string">'i18n'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'translations'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'*'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\i18n\PhpMessageSource'</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Now you can use categories without configuring each one, which is similar to Yii 1.1 behavior. Messages for the category will be loaded from a file under the default translation <code v-pre>basePath</code> that is <code v-pre>@app/messages</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'not_specified_category'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'message from unspecified category'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The message will be loaded from <code v-pre>@app/messages/&lt;LanguageCode&gt;/not_specified_category.php</code>.</p>
<h3 id="translating-module-messages" tabindex="-1"><a class="header-anchor" href="#translating-module-messages"><span>Translating module messages</span></a></h3>
<p>If you want to translate the messages for a module and avoid using a single translation file for all the messages, you can do it like the following:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>modules<span class="token punctuation">\</span>users</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Yii</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Module</span> <span class="token keyword">extends</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>base<span class="token punctuation">\</span>Module</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$controllerNamespace</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'app\modules\users\controllers'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword static-context">parent</span><span class="token operator">::</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">registerTranslations</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">registerTranslations</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">i18n</span><span class="token operator">-></span><span class="token property">translations</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'modules/users/*'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\i18n\PhpMessageSource'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'sourceLanguage'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'en-US'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'basePath'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@app/modules/users/messages'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'fileMap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'modules/users/validation'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'validation.php'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'modules/users/form'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'form.php'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token operator">...</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">function</span> <span class="token function-definition function">t</span><span class="token punctuation">(</span><span class="token variable">$category</span><span class="token punctuation">,</span> <span class="token variable">$message</span><span class="token punctuation">,</span> <span class="token variable">$params</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$language</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'modules/users/'</span> <span class="token operator">.</span> <span class="token variable">$category</span><span class="token punctuation">,</span> <span class="token variable">$message</span><span class="token punctuation">,</span> <span class="token variable">$params</span><span class="token punctuation">,</span> <span class="token variable">$language</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>In the example above we are using wildcard for matching and then filtering each category per needed file. Instead of using <code v-pre>fileMap</code>, you can simply use the convention of the category mapping to the same named file. Now you can use <code v-pre>Module::t('validation', 'your custom validation message')</code> or <code v-pre>Module::t('form', 'some form label')</code> directly.</p>
<h3 id="translating-widgets-messages" tabindex="-1"><a class="header-anchor" href="#translating-widgets-messages"><span>Translating widgets messages</span></a></h3>
<p>The same rule as applied for Modules above can be applied for widgets too, for example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>widgets<span class="token punctuation">\</span>menu</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>base<span class="token punctuation">\</span>Widget</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Yii</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Menu</span> <span class="token keyword">extends</span> <span class="token class-name">Widget</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword static-context">parent</span><span class="token operator">::</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">registerTranslations</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">registerTranslations</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$i18n</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">i18n</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$i18n</span><span class="token operator">-></span><span class="token property">translations</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'widgets/menu/*'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\i18n\PhpMessageSource'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'sourceLanguage'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'en-US'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'basePath'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@app/widgets/menu/messages'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'fileMap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'widgets/menu/messages'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'messages.php'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">echo</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">render</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'index'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">function</span> <span class="token function-definition function">t</span><span class="token punctuation">(</span><span class="token variable">$category</span><span class="token punctuation">,</span> <span class="token variable">$message</span><span class="token punctuation">,</span> <span class="token variable">$params</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$language</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token function">t</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'widgets/menu/'</span> <span class="token operator">.</span> <span class="token variable">$category</span><span class="token punctuation">,</span> <span class="token variable">$message</span><span class="token punctuation">,</span> <span class="token variable">$params</span><span class="token punctuation">,</span> <span class="token variable">$language</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Instead of using <code v-pre>fileMap</code> you can simply use the convention of the category mapping to the same named file. Now you can use <code v-pre>Menu::t('messages', 'new messages {messages}', ['{messages}' =&gt; 10])</code> directly.</p>
<blockquote>
<p>Note: For widgets you also can use i18n views, with the same rules as for controllers being applied to them too.</p>
</blockquote>
<h3 id="translating-framework-messages" tabindex="-1"><a class="header-anchor" href="#translating-framework-messages"><span>Translating framework messages</span></a></h3>
<p>Yii comes with the default translation messages for validation errors and some other strings. These messages are all in the category <code v-pre>yii</code>. Sometimes you want to correct the default framework message translation for your application. In order to do so, configure the <code v-pre>i18n</code> <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/structure-application-components.md" target="_blank" rel="noopener noreferrer">application component</a> like the following:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'i18n'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'translations'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'yii'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\i18n\PhpMessageSource'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'sourceLanguage'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'en-US'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'basePath'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@app/messages'</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Now you can place your adjusted translations to <code v-pre>@app/messages/&lt;language&gt;/yii.php</code>.</p>
<h3 id="handling-missing-translations" tabindex="-1"><a class="header-anchor" href="#handling-missing-translations"><span>Handling missing translations</span></a></h3>
<p>Even if the translation is missing from the source, Yii will display the requested message content. Such behavior is very convenient in case your raw message is a valid verbose text. However, sometimes it is not enough. You may need to perform some custom processing of the situation, when the requested translation is missing from the source. This can be achieved using the [[yii\i18n\MessageSource::EVENT_MISSING_TRANSLATION|missingTranslation]]-event of [[yii\i18n\MessageSource]].</p>
<p>For example, you may want to mark all the missing translations with something notable, so that they can be easily found at the page. First you need to setup an event handler. This can be done in the application configuration:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'i18n'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'translations'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'app*'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\i18n\PhpMessageSource'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'fileMap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'app'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app.php'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'app/error'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'error.php'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'on missingTranslation'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'app\components\TranslationEventHandler'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'handleMissingTranslation'</span><span class="token punctuation">]</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Now you need to implement your own event handler:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>components</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>i18n<span class="token punctuation">\</span>MissingTranslationEvent</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">TranslationEventHandler</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">function</span> <span class="token function-definition function">handleMissingTranslation</span><span class="token punctuation">(</span><span class="token class-name type-declaration">MissingTranslationEvent</span> <span class="token variable">$event</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$event</span><span class="token operator">-></span><span class="token property">translatedMessage</span> <span class="token operator">=</span> <span class="token string double-quoted-string">"@MISSING: <span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$event</span><span class="token operator">-></span><span class="token property">category</span><span class="token punctuation">}</span></span>.<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$event</span><span class="token operator">-></span><span class="token property">message</span><span class="token punctuation">}</span></span> FOR LANGUAGE <span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$event</span><span class="token operator">-></span><span class="token property">language</span><span class="token punctuation">}</span></span> @"</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>If [[yii\i18n\MissingTranslationEvent::translatedMessage]] is set by the event handler it will be displayed as the translation result.</p>
<blockquote>
<p>Note: each message source handles its missing translations separately. If you are using several message sources and wish them to treat the missing translations in the same way, you should assign the corresponding event handler to each of them.</p>
</blockquote>
<h3 id="using-the-message-command" tabindex="-1"><a class="header-anchor" href="#using-the-message-command"><span>Using the <code v-pre>message</code> command</span></a></h3>
<p>Translations can be stored in [[yii\i18n\PhpMessageSource|php files]], [[yii\i18n\GettextMessageSource|.po files]] or in a [[yii\i18n\DbMessageSource|database]]. See specific classes for additional options.</p>
<p>First of all you need to create a configuration file. Decide where you want to store it and then issue the command</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./yii message/config-template path/to/config.php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Open the created file and adjust the parameters to fit your needs. Pay special attention to:</p>
<ul>
<li><code v-pre>languages</code>: an array representing what languages your app should be translated to;</li>
<li><code v-pre>messagePath</code>: path where to store message files, which should match the <code v-pre>i18n</code>‘s <code v-pre>basePath</code> parameter stated in config.</li>
</ul>
<p>You may also use ‘./yii message/config’ command to dynamically generate configuration file with specified options via cli. For example, you can set <code v-pre>languages</code> and <code v-pre>messagePath</code> parameters like the following:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./yii message/config <span class="token parameter variable">--languages</span><span class="token operator">=</span>de,ja <span class="token parameter variable">--messagePath</span><span class="token operator">=</span>messages path/to/config.php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>To get list of available options execute next command:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./yii <span class="token builtin class-name">help</span> message/config</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Once you’re done with the configuration file you can finally extract your messages with the command:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./yii message path/to/config.php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Also, you may use options to dynamically change parameters for extraction.</p>
<p>You will then find your files (if you’ve chosen file based translations) in your <code v-pre>messagePath</code> directory.</p>
<h2 id="view-translation" tabindex="-1"><a class="header-anchor" href="#view-translation"><span>View Translation</span></a></h2>
<p>Instead of translating individual text messages, sometimes you may want to translate a whole view script. To achieve this goal, simply translate the view and save it under a subdirectory whose name is the same as target language. For example, if you want to translate the view script <code v-pre>views/site/index.php</code> and the target language is <code v-pre>ru-RU</code>, you may translate the view and save it as the file <code v-pre>views/site/ru-RU/index.php</code>. Now whenever you call [[yii\base\View::renderFile()]] or any method that invoke this method (e.g. [[yii\base\Controller::render()]]) to render the view <code v-pre>views/site/index.php</code>, it will end up rendering the translated view <code v-pre>views/site/ru-RU/index.php</code>, instead.</p>
<blockquote>
<p>Note: If the [[yii\base\Application::$language|target language]] is the same as [[yii\base\Application::$sourceLanguage|source language]] original view will be rendered regardless of presence of translated view.</p>
</blockquote>
<h2 id="formatting-date-and-number-values" tabindex="-1"><a class="header-anchor" href="#formatting-date-and-number-values"><span>Formatting Date and Number Values</span></a></h2>
<p>See the <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/output-formatting.md" target="_blank" rel="noopener noreferrer">Data Formatting</a> section for details.</p>
<h2 id="setting-up-php-environment" tabindex="-1"><a class="header-anchor" href="#setting-up-php-environment"><span>Setting Up PHP Environment</span></a></h2>
<p>Yii uses the <a href="https://www.php.net/manual/en/book.intl.php" target="_blank" rel="noopener noreferrer">PHP intl extension</a> to provide most of its I18N features, such as the date and number formatting of the [[yii\i18n\Formatter]] class and the message formatting using [[yii\i18n\MessageFormatter]]. Both classes provide a fallback mechanism when the <code v-pre>intl</code> extension is not installed. However, the fallback implementation only works well for English target language. So it is highly recommended that you install <code v-pre>intl</code> when I18N is needed.</p>
<p>The <a href="https://www.php.net/manual/en/book.intl.php" target="_blank" rel="noopener noreferrer">PHP intl extension</a> is based on the <a href="https://icu.unicode.org/" target="_blank" rel="noopener noreferrer">ICU library</a> which provides the knowledge and formatting rules for all different locales. Different versions of ICU may produce different formatting result of date and number values. To ensure your website produces the same results across all environments, it is recommended that you install the same version of the <code v-pre>intl</code> extension (and thus the same version of ICU) in all environments.</p>
<p>To find out which version of ICU is used by PHP, you can run the following script, which will give you the PHP and ICU version being used.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token string double-quoted-string">"PHP: "</span> <span class="token operator">.</span> <span class="token constant">PHP_VERSION</span> <span class="token operator">.</span> <span class="token string double-quoted-string">"\n"</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token string double-quoted-string">"ICU: "</span> <span class="token operator">.</span> <span class="token constant">INTL_ICU_VERSION</span> <span class="token operator">.</span> <span class="token string double-quoted-string">"\n"</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token string double-quoted-string">"ICU Data: "</span> <span class="token operator">.</span> <span class="token constant">INTL_ICU_DATA_VERSION</span> <span class="token operator">.</span> <span class="token string double-quoted-string">"\n"</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>It is also recommended that you use an ICU version equal or greater than version 49. This will ensure you can use all the features described in this document. For example, an ICU version below 49 does not support using <code v-pre>#</code> placeholders in plural rules. Please refer to <a href="https://icu.unicode.org/download" target="_blank" rel="noopener noreferrer">icu.unicode.org/download</a> for a complete list of available ICU versions. Note that the version numbering has changed after the 4.8 release (e.g., ICU 4.8, ICU 49, ICU 50, etc.)</p>
<p>Additionally the information in the time zone database shipped with the ICU library may be outdated. Please refer to the <a href="https://unicode-org.github.io/icu/userguide/datetime/timezone/#updating-the-time-zone-data" target="_blank" rel="noopener noreferrer">ICU manual</a> for details on updating the time zone database. While for output formatting the ICU timezone database is used, the time zone database used by PHP may be relevant too. You can update it by installing the latest version of the <a href="https://pecl.php.net/package/timezonedb" target="_blank" rel="noopener noreferrer">pecl package <code v-pre>timezonedb</code></a>.</p>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


